package org.grenobus;

import java.util.concurrent.ExecutorService;

import org.grenobus.html.Field;
import org.grenobus.html.FieldSelect;
import org.grenobus.html.SemitagHtmlPage;
import org.grenobus.html.FieldSelect.SelectPair;

import android.app.ProgressDialog;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

public class LinesView extends LinearLayout {
	
	Grenobus mApp;
	View     mView;

	SemitagHtmlPage fPageLignes, fPageArrets;
	
	static SemitagHtmlPage fPageHoraires;
	
	public LinesView(Grenobus app) {
		super(app);
		mApp = app;
		
		LayoutInflater inflater = app.getLayoutInflater();
		mView = inflater.inflate(R.layout.lines_view, this);
		
		createLinesView();
	}
	
	private void createLinesView() {
	    final ExecutorService threadPool = Grenobus.getThreadPool();		
		final Spinner spin = (Spinner)mView.findViewById(R.id.Spinner01);
		final ListView spinArrets = (ListView)mView.findViewById(R.id.ListView02);						
		final ProgressDialog dialog = ProgressDialog.show(mApp, "", 
				"Chargement des lignes. Patientez SVP...", true);
		
		spinArrets.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1,
					final int position, long arg3) {

				final ProgressDialog dialog = ProgressDialog.show(mApp, "", 
						"Chargement des horaires. Patientez SVP...", true);

				threadPool.execute(new Runnable() {

					public void run() {
						fPageHoraires = fPageArrets.postForm("selection_arret", position);
						Intent intent = new Intent(Intent.ACTION_DEFAULT);
				        intent.setClassName(mApp, "org.grenobus.ScheduleActivity");
				        mApp.startActivity(intent);
						dialog.dismiss();
					}});		
			}
		});
		
		spin.setOnItemSelectedListener(
				new OnItemSelectedListener() {

					public void onItemSelected(AdapterView<?> parent, View view, final int position, long id) {

						if (position == 0) return;
						
						final ProgressDialog dialog = ProgressDialog.show(mApp, "", 
								"Chargement des arrets. Patientez SVP...", true);

						threadPool.execute(new Runnable() {

							public void run() {
								fPageArrets = fPageLignes.postForm("selection_ligne", position-1);
								
								FieldSelect arrets = null;
								for (Field f : fPageArrets.getForm().getFields()) {
									if (f instanceof FieldSelect && "selection_arret".equals(f.getName())) {
										arrets = (FieldSelect)f;
										break;
									}
								}

								if (arrets != null) {
									String list[] = new String[arrets.getChoices().size()];
									int i = 0;
									for (SelectPair pair: arrets.getChoices()) {
										list[i++] = pair.display;
									}

									final ArrayAdapter<String> adapter = new ArrayAdapter<String>(mApp, android.R.layout.simple_list_item_1, list);
									spinArrets.post(new Runnable() {
										public void run() {
											spinArrets.setAdapter(adapter);
											dialog.dismiss();
										}
									});
								} else {
									spinArrets.post(new Runnable() {
										public void run() {
											dialog.dismiss();
											Toast.makeText(mApp, "Erreur durant la récupération des stations.", Toast.LENGTH_LONG).show();
										}
									});
								}
								dialog.dismiss();
							}});
					}

					public void onNothingSelected(AdapterView<?> arg0) {
					}
				}
		);

		threadPool.execute(new Runnable() {

			public void run() {
				fPageLignes = new SemitagHtmlPage("http://semitag.transvalley.com/Horaires/Horaires_Ligne/H_Ligne.asp");
				FieldSelect lignes = null;
				for (Field f : fPageLignes.getForm().getFields()) {
					if (f instanceof FieldSelect && "selection_ligne".equals(f.getName())) {
						lignes = (FieldSelect)f;
						break;
					}
				}

				if (lignes != null) {
					String list[] = new String[lignes.getChoices().size()+1];
					int i = 1;
					list[0] = "Choisissez une ligne...";
					for (SelectPair pair: lignes.getChoices()) {
						list[i++] = pair.display;
					}

					final ArrayAdapter<String> adapter = new ArrayAdapter<String>(mApp, android.R.layout.simple_spinner_item, list);
					adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
					spin.post(new Runnable() {
						public void run() {
							spin.setAdapter(adapter);
							dialog.dismiss();
						}
					});
				} else {
					spin.post(new Runnable() {
						public void run() {
							dialog.dismiss();
							Toast.makeText(mApp, "Erreur durant la récupération des stations.", Toast.LENGTH_LONG).show();
						}
					});
				}
			}
		});
	}
}
